package cn.gong.store.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.gong.json.utils.BeanUtils;
import cn.gong.store.bean.JsonCache;
import cn.gong.utils.LogUtils;

public class CacheUtils {

	// cahce方法

	public static void jsonCacheByMd5(String data, String path, Object name) {
		jsonCacheByMd5(data, path, String.valueOf(name));
	}

	public static void jsonCacheByMd5(String data, String path, String name) {
		jsonCache(data, path, Md5Utils.MD5(name));
	}

	public static void jsonCache(String data, String path, Object name) {
		jsonCache(data, path, String.valueOf(name));
	}

	public static void jsonCache(String data, String path, String name) {
		cacheObj(new JsonCache(data), path, name);
	}

	public static void cacheObjByMd5(Object obj, String path, Object name) {
		cacheObjByMd5(obj, path, String.valueOf(name));
	}

	public static void cacheObjByMd5(Object obj, String path, String name) {
		cacheObj(obj, path, Md5Utils.MD5(name));
	}

	public static void cacheObj(Object obj, String path, Object name) {
		cacheObj(obj, path, String.valueOf(name));
	}

	public static void cacheObj(Object obj, String path, String name) {
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		try {
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			fos = new FileOutputStream(new File(file, name));
			oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			fos.flush();
		} catch (Exception e) {
			LogUtils.exception(e);
		} finally {
			try {
				if (oos != null) {
					oos.close();
				}
			} catch (IOException e) {
				LogUtils.exception(e);
			} finally {
				try {
					if (fos != null) {
						fos.close();
					}
				} catch (IOException e) {
					LogUtils.exception(e);
				}
			}
		}
	}

	// get方法们

	public static <T> ArrayList<T> getBeanArrayByMd5(Class<T> clazz, String path, Object name) {
		return getBeanArrayByMd5(clazz, path, String.valueOf(name));
	}

	public static <T> ArrayList<T> getBeanArrayByMd5(Class<T> clazz, String path, String name) {
		return getBeanArray(clazz, path, Md5Utils.MD5(name));
	}

	public static <T> ArrayList<T> getBeanArray(Class<T> clazz, String path, Object name) {
		return getBeanArray(clazz, path, String.valueOf(name));
	}

	public static <T> ArrayList<T> getBeanArray(Class<T> clazz, String path, String name) {
		JsonCache cacheObj = getJsonCache(path, name);
		if (cacheObj != null) {
			String data = cacheObj.getData();
			if (cacheObj.isNowBean()) {
				return BeanUtils.getNowBeanArray(clazz, data);
			} else {
				return BeanUtils.getOldBeanArray(clazz, data);
			}
		} else {
			return null;
		}
	}

	public static <T> T getBeanByMd5(Class<T> clazz, String path, Object name) {
		return getBeanByMd5(clazz, path, String.valueOf(name));
	}

	public static <T> T getBeanByMd5(Class<T> clazz, String path, String name) {
		return getBean(clazz, path, Md5Utils.MD5(name));
	}

	public static <T> T getBean(Class<T> clazz, String path, Object name) {
		return getBean(clazz, path, String.valueOf(name));
	}

	public static <T> T getBean(Class<T> clazz, String path, String name) {
		JsonCache cacheObj = getJsonCache(path, name);
		try {
			if (cacheObj.isNowBean()) {
				return BeanUtils.nowBean(clazz, new JSONObject(cacheObj.getData()));
			} else {
				return BeanUtils.oldBean(clazz, new JSONObject(cacheObj.getData()));
			}
		} catch (Exception e) {
			LogUtils.exception(e);
			return null;
		}
	}

	public static JsonCache getJsonCache(String path, Object name) {
		return getJsonCache(path, String.valueOf(name));
	}

	public static JsonCache getJsonCache(String path, String name) {
		return (JsonCache) getObj(path, name);
	}

	public static JsonCache getJsonCacheByMd5(String path, Object name) {
		return getJsonCacheByMd5(path, String.valueOf(name));
	}

	public static JsonCache getJsonCacheByMd5(String path, String name) {
		return getJsonCache(path, Md5Utils.MD5(name));
	}

	public static Object getObjByMd5(String path, Object name) {
		return getObjByMd5(path, String.valueOf(name));
	}

	public static Object getObjByMd5(String path, String name) {
		return getObj(path, Md5Utils.MD5(name));
	}

	public static Object getObj(String path, Object name) {
		return getObj(path, String.valueOf(name));
	}

	public static Object getObj(String path, String name) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream(path + name);
			ois = new ObjectInputStream(fis);
			Object readObject = ois.readObject();
			fis.close();
			ois.close();
			return readObject;
		} catch (Exception e) {
			LogUtils.exception(e);
			return null;
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (Exception e) {
				LogUtils.exception(e);
			} finally {
				try {
					if (ois != null) {
						ois.close();
					}
				} catch (Exception e) {
					LogUtils.exception(e);
				}
			}
		}
	}
}
